﻿Imports System
Imports System.IO
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text
Imports Excel = Microsoft.Office.Interop.Excel

Public Class FileFunction
    'Ham moi lam ngay 1/4/2013
    'lấy các giá trị HashTable từ file Excel
    Public Shared Function GetHashTableFromExcelSheet(ByVal xlSheet As Excel.Worksheet) As Hashtable
        Dim kq As New Hashtable(CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant)
        Dim soDong As Integer = xlSheet.Range("A65536").End(Excel.XlDirection.xlUp).Row
        For iDong As Integer = 2 To soDong
            'cot 1 la gia tri index
            Dim index As String = xlSheet.Range(xlSheet.Cells(iDong, 1), xlSheet.Cells(iDong, 1)).Value.ToString.Trim
            If (kq.Contains(index) = False) Then
                'cot 2 la value
                Dim giatri As String = xlSheet.Range(xlSheet.Cells(iDong, 2), xlSheet.Cells(iDong, 2)).Value.ToString.Trim
                kq.Add(index, giatri)
            End If
        Next
        Return kq
    End Function
    Public Shared Function GetFileName(ByVal file As String)
        Dim result As String = ""
        If (Not String.IsNullOrEmpty(file)) Then
            Dim parts As String() = file.Split(New String() {"\"}, StringSplitOptions.RemoveEmptyEntries)
            result = parts(parts.Length - 1)
        End If
        Return result
    End Function
    Public Shared Function Doc_File(ByVal fileName As String) As String
        Dim kq As String = ""
        Dim Bo_doc As StreamReader = New StreamReader(fileName)
        kq = Bo_doc.ReadToEnd()
        Bo_doc.Close()
        Return kq
    End Function
    Public Shared Function Ghi_File(ByVal fileName As String, ByVal s As String) As Boolean
        Dim kq As Boolean = True
        Try
            Using Bo_Ghi As New StreamWriter(fileName)
                Bo_Ghi.Write(s)
                Bo_Ghi.Flush()
                Bo_Ghi.Close()
            End Using
        Catch Loi As Exception
            kq = False
        End Try
        Return kq
    End Function
    Public Shared Function ReadFileToHashTable(ByVal fileName As String) As Hashtable
        Dim h As Hashtable = New Hashtable(CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant)
        Try
            Dim fs As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read)
            Dim sr As StreamReader = New StreamReader(fs, Encoding.UTF8)

            While Not sr.EndOfStream
                Dim s As String = sr.ReadLine()
                Dim index1 As Integer = s.IndexOf(" ")
                Dim qLabel As String = s.Substring(0, index1)
                s = s.Substring(index1 + 1)
                h.Add(qLabel, s)
            End While
            sr.Close()
            fs.Close()
        Catch ex As Exception

        End Try
        Return h
    End Function
    Public Shared Function ReadFileToHashTable2(ByVal fileName As String) As Hashtable
        'Doc HashTable theo format Index | Gia tri
        Dim h As Hashtable = New Hashtable()
        Try
            Dim fs As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read)
            Dim sr As StreamReader = New StreamReader(fs, Encoding.Unicode)

            While Not sr.EndOfStream
                Dim s As String = sr.ReadLine()
                Dim index1 As Integer = s.IndexOf("|")
                Dim qLabel As String = s.Substring(0, index1).Trim
                s = s.Substring(index1 + 1).Trim
                h.Add(qLabel, s)
            End While
            sr.Close()
            fs.Close()
        Catch ex As Exception

        End Try
        Return h
    End Function
End Class
